package nl.zuiderfestival.raydance.view.canvas;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.Observable;
import java.util.Observer;

import nl.zuiderfestival.raydance.model.zones.Zone;

public class ZoneShape implements Observer {
	private static final double THUMB_SIZE = 6.0;

	private final double resizeThumbSize;
	
	private Zone zone;
	private Rectangle rectangle;
	private boolean selected;
	
	public ZoneShape(Zone zone, Rectangle rectangle) {
		this(zone, rectangle, THUMB_SIZE);
	}
	
	public ZoneShape(Zone zone, Rectangle rectangle, double resizeThumbSize) {
		super();
		this.resizeThumbSize = resizeThumbSize;
		this.zone = zone;
		this.rectangle = rectangle;
		
		update(null, null);
		zone.addObserver(this);
	}

	public Rectangle getRectangle() {
		return rectangle;
	}

	public void update(Observable o, Object arg) {
		rectangle.setLocation(zone.getLocation().x, zone.getLocation().y);
		rectangle.setSize(zone.getSize().width, zone.getSize().height);
	}

	public void setLocation(double x, double y) {
		zone.setLocation(new Point((int)x,(int)y));
		
	}

	public void setSize(double width, double height) {
		zone.setSize(new Dimension((int)width,(int)height));
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof ZoneShape) {
			ZoneShape other = (ZoneShape)obj;
			return zone.equals(other.zone);
		} else {
			return zone.equals(obj);
		}
	}
	
	public String toString() {
		return "ZoneShape["+zone+"]";
	}

	public Zone getZone() {
		return zone;
	}

	public void draw(Graphics2D g2d) {
		double x = rectangle.getX();
		double y = rectangle.getY();
		double w = rectangle.getWidth();
		double h = rectangle.getHeight();
		
		g2d.setColor(Color.white);
		
		if (zone.isEnabled()) {
			g2d.setColor(Color.red);
			g2d.fill(new Rectangle2D.Double(x, y, w, h));
		} else {
			g2d.setColor(isSelected()?Color.green:Color.white);
			g2d.draw(new Rectangle2D.Double(x, y, w, h));
		}
		
		g2d.setColor(isSelected()?Color.green:Color.white);
		g2d.drawChars(zone.toString().toCharArray(), 0, zone.toString().length(), (int) x+2, (int) (y+12));
		
		if (isSelected()) {
			g2d.setColor(Color.green);
			g2d.fill(new Rectangle.Double(x + w - resizeThumbSize, y + h - resizeThumbSize, resizeThumbSize, resizeThumbSize));
		}
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public boolean isMouseOverResizeThumb(Point point) {
		double x = rectangle.getX();
		double y = rectangle.getY();
		double w = rectangle.getWidth();
		double h = rectangle.getHeight();
		
		boolean result = false;
		if (point.x >= x + w - resizeThumbSize && point.x <= x + w) {
			if (point.y >= y + h - resizeThumbSize && point.y <= y + h) {
				result = true;
			}
		}
		return result;
	}
	
}
